package com.ruoyi.project.product.manager.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.manager.domain.Manager;
import com.ruoyi.project.product.manager.service.IManagerService;
import com.ruoyi.project.product.series.domain.Series;
import com.ruoyi.project.product.series.service.ISeriesService;
import com.ruoyi.project.product.specification.domain.Specification;
import com.ruoyi.project.product.specification.service.ISpecificationService;
import com.ruoyi.project.product.style.domain.Style;
import com.ruoyi.project.product.style.service.IStyleService;
import com.ruoyi.project.product.url.domain.Url;
import com.ruoyi.project.product.url.service.IUrlService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品管理操作处理
 * @author Mai
 */
@Controller
@RequestMapping("product/manager")
public class ManagerController extends BaseController {

    private String prefix="product/manager";

    @Autowired
    private IManagerService managerService;

    @Autowired
    private ISeriesService seriesService;

    @Autowired
    private IStyleService styleService;

    @Autowired
    private ISpecificationService specificationService;

    @Autowired
    private IUrlService urlService;

    /**
     * 跳转到查询页面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("product:manager:view")
    public String manager(){
        return prefix + "/manager";
    }

    /**
     * 产品管理查询
     * @param manager
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("product:manager:list")
    public TableDataInfo list(Manager manager){
        startPage();
        System.out.println(managerService.selectManagerList(manager));
        List<Manager> list = managerService.selectManagerList(manager);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(ModelMap mmap){
        mmap.put("series",seriesService.selectSeriesList(new Series()));
        mmap.put("style",styleService.selectStyleList(new Style()));
        mmap.put("specification",specificationService.selectSpecificationList(new Specification()));
        mmap.put("url",urlService.selectUrlList(new Url()));
        return prefix + "/add";
    }

    /**
     * 产品管理新增
     * @param manager
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions("product:manager:add")
    public AjaxResult addSave(Manager manager){
        //判断1是否等于返回的值，如果是等于1 ，就表中存在，不能添加数据。
        if(UserConstants.SERIES_NAME_NOT_UNIQUE.equals(managerService.checkManagerNumUnique(manager))){
            return error("新增系列"+manager.getManagerName()+"失败，系列名称已存在");
        }
        return toAjax(managerService.insertManager(manager));
    }

    /**
     * 产品管理修改
     * @param managerId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{managerId}")
    public String edit(@PathVariable("managerId") Long managerId, ModelMap mmap){
        mmap.put("manager",managerService.selectManagerById(managerId));
        mmap.put("series",seriesService.selectSeriesList(new Series()));
        mmap.put("style",styleService.selectStyleList(new Style()));
        mmap.put("specification",specificationService.selectSpecificationList(new Specification()));
        mmap.put("url",urlService.selectUrlList(new Url()));
        return prefix + "/edit";
    }

    /**
     * 产品管理修改保存
     * @param manager
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions("product:manager:edit")
    public AjaxResult editSave(Manager manager){
        return toAjax(managerService.updateManager(manager));
    }

    @GetMapping("/detail/{managerId}")
    public String detail(@PathVariable("managerId") Long managerId,ModelMap mmap){
        mmap.put("manager",managerService.selectManagerById(managerId));
        mmap.put("series",seriesService.selectSeriesList(new Series()));
        mmap.put("style",styleService.selectStyleList(new Style()));
        mmap.put("specification",specificationService.selectSpecificationList(new Specification()));
        return prefix + "/detail";
    }


    /**
     * 产品管理批量删除操作
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    @RequiresPermissions("product:manager:remove")
    public AjaxResult remove(String ids){
        try {
            return toAjax(managerService.deleteManagerByIds(ids));
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }



    /**
     * 校验产品型号是否存在
     * @param manager
     * @return
     */
    @ResponseBody
    @PostMapping("/checkManagerNumUnique")
    public String checkManagerNumUnique(Manager manager){
        return managerService.checkManagerNumUnique(manager);
    }


    /**
     * dom操作更改图片
     * @param urlId
     * @return
     */
    @ResponseBody
    @PostMapping("/changeImagesUrl")
    public AjaxResult changeImagesUrl(Integer urlId){
        return AjaxResult.success(urlService.selectUrlById(urlId));
    }

}
